package com.eric.text;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.fnlp.nlp.cn.CNFactory;
import org.fnlp.nlp.cn.tag.NERTagger;
import org.jsoup.nodes.Document;

public class FNLPNerWorker extends AbstractTextWorker {
	NERTagger tag;

	public FNLPNerWorker(Object input) {
		super(input);
	}

	@Override
	public void produce() {
		initThis();
		HashMap<String, String> map = new HashMap<String, String>();
		tag.tag(text, map);
		System.out.println((map));
		System.out.println(trimEntity(map));
	}

	@Override
	public List<String> produce(String text, Document doc) {
		return null;
	}

	@Override
	protected void initThis() {
		try {
			tag = new NERTagger("models/seg.m", "models/pos.m");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (customWords.size() > 0)
			CNFactory.addDict(customWords);

	}

	@Override
	public List<String> getContext() {
		return null;
	}

	public Map<String, String> trimEntity(Map<String, String> map) {
		HashSet<String> set = new HashSet<>();
		for (Entry<String, String> entry : map.entrySet()) {
			set.add(entry.getKey());
		}
		HashSet<String> trimmedSet = trimSet(set);
		HashMap<String, String> trimmedMap = new HashMap<String, String>();
		for (String s : trimmedSet) {
			trimmedMap.put(s, map.get(s));
		}
		return trimmedMap;
	}

	/**
	 * 缩小清单，规则：如某一元素是另一元素的子集，则剔除
	 * 
	 * @param set
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public HashSet<String> trimSet(HashSet<String> set) {
		HashSet<String> set2 = (HashSet<String>) set.clone();

		HashSet<String> set3 = new HashSet<>();
		for (String s : set) {
			boolean t = true;
			for (String ss : set2) {
				if (!ss.equals(s) && ss.contains(s))// 元素1不等于元素2,且包含于元素2,则认为是子集
					t = false;
			}
			if (t)
				set3.add(s);
		}
		return set3;
	}

}
